package com.yjt.fresh.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yjt.fresh.activity.AtyLogin;
import com.yjt.fresh.utils.ActivityStackControlUtil;

import butterknife.Unbinder;

import static com.yjt.fresh.base.BaseSimpleActivity.LOGIN_REQUEST_CODE;


/**
 * @author Gxcheng
 * @desc
 * @time 2016/11/1 0001 9:00
 */
public abstract class BaseSimpleFragment extends Fragment {
    protected static final int LOGIN_REQUEST_CODE = 1008;
    protected String TAG;
    //    protected FireApplication mApp;
    protected Activity mContext;
    protected View mContentView;

    protected int mPageIndex = 1;
    protected int mPageSize = 10;
    protected int mPageCount;//总页数
    private Unbinder unbinder;

    public void onAttach(Context activity) {
        super.onAttach(activity);
        TAG = this.getClass().getSimpleName();
        mContext=getActivity();
        ActivityStackControlUtil.add(mContext);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 避免多次从xml中加载布局文件
        if (mContentView == null) {
            mContentView = LayoutInflater.from(getActivity()).inflate(getContentView(), null);
            initView(savedInstanceState,mContentView);
            initEvents();
        } else {
            ViewGroup parent = (ViewGroup) mContentView.getParent();
            if (parent != null) {
                parent.removeView(mContentView);
            }
        }

        return mContentView;
    }

    protected void toLogin(){
        Intent intent = new Intent(mContext, AtyLogin.class);
        startActivityForResult(intent,LOGIN_REQUEST_CODE);
    }
//    protected void setContentView(@LayoutRes int layoutResID) {
//        mContentView = LayoutInflater.from(getActivity()).inflate(layoutResID, null);
//        unbinder= ButterKnife.bind(this,mContentView);
//    }

    /*获取xml布局*/
    protected int getContentView() {
        return 0;
    }


    /**
     * 初始化View控件
     */
    protected abstract void initView(Bundle savedInstanceState,View view);

    /**
     * 给View控件添加事件监听器
     */
    protected abstract void initEvents();

    /**
     * 处理业务逻辑，状态恢复等操作
     *
     * @param savedInstanceState
     */
//    protected abstract void processLogic(Bundle savedInstanceState);

    /**
     * 当fragment对用户可见时，会调用该方法，可在该方法中懒加载网络数据
     */
//    protected abstract void onUserVisible();

    /**
     * 查找View
     *
     * @param id   控件的id
     * @param <VT> View类型
     * @return
     */
    protected <VT extends View> VT getViewById(@IdRes int id) {
        return (VT) mContentView.findViewById(id);
    }

    protected void showToast(String text) {
//        ToastUtil.show(text);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mContext=null;
    }

    /**
     * onDestroyView中进行解绑操作
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
